﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Kendo.Mvc.Infrastructure;
using Kendo.Mvc.UI;
using Kendo.Mvc.UI.Fluent;
using Kendo.Mvc.UI.Html;
using MN.Framework.Common;
using MN.Framework.Common.Service;

namespace Mn.Framework.Web.Mvc
{
    public static class HtmlHelperExtension
    {
        //public static GridBuilder<TModel> GridMaster<TModel>(this MnWidgetFactory mnWidget, string controllerName) where TModel : BaseEntity
        //{
        //    var grid = mnWidget.MnGrid<TModel>()
        //         .Name("Grid")
        //         .Columns(columns =>
        //         {
        //             columns.Command(command =>
        //             {
        //                 command.Edit().Text("ویرایش");
        //                 command.Destroy().Text("حذف");
        //             }).Width(90);
        //             foreach (var clmn in ServiceFactory.GetColumns<TModel>())
        //             {
        //                 columns.Bound(clmn.Name).Title(clmn.HeaderText).Width(clmn.Width(100));
        //             }
        //             //columns.AutoGenerate(true);

        //         })
        //         .Pageable()
        //         .ToolBar(toolbar => toolbar.Create().Text("آیتم جدید"))
        //         .Sortable()
        //         .Groupable()
        //         .Filterable()
        //         .DataSource(dataSource => dataSource
        //             .Ajax()
        //             .Read(read => read.Action("Grid_Read", controllerName))
        //             .Update(update => update.Action("Edit", controllerName))
        //             .Create("Create", controllerName)
        //             .Destroy("Delete", controllerName)
        //             .Model(model => model.Id("ID"))
        //             .PageSize(20)
        //         );
        //    return grid;
        //}

        //public static GridBuilder<TDetailType> GridDetailServer<TDetailType>(GridBuilder<BaseEntity> masterGrid, Expression<TDetailType> expression, HtmlHelper helper, string controllerName)
        //    where TDetailType : BaseEntity
        //{
        //    //var controllerName=typeof(T).Name.Replace("Model","")
        //    var fk = expression.Name;
        //    return helper.Kendo().Grid<TDetailType>()
        //          .Name("grid_#=ID#")
        //          .Columns(columns =>
        //          {
        //              columns.Command(command =>
        //              {
        //                  command.Edit();
        //                  command.Destroy();
        //              });
        //              columns.AutoGenerate(true);
        //          })
        //          .Editable(edit => edit.ConfirmDelete("آیا از حذف مورد اطمینان دارید؟"))
        //          .DataSource(dataSource => dataSource
        //              .Ajax()
        //              .PageSize(5)
        //              .Destroy("Delete", controllerName)
        //              .Read(read => read.Action("Grid_Read_Detail", controllerName, new { fk = "#=ID#" }))
        //              .Model(model => model.Id("ID"))
        //          )
        //          .Pageable()
        //          .Sortable();
        //}

        //public static MvcHtmlString GridDetailAjax<TM, T>(this MnGridBuilder<TM> masterGrid, string refProperty)
        //    where T : BaseEntity
        //    where TM : BaseEntity
        //{
        //    var controllerName = masterGrid.Helper.ViewContext.HttpContext.Request.RequestContext.RouteData.Values["controller"].ToString();
        //    return GridDetailAjax<TM, T>(masterGrid, controllerName, refProperty);
        //}

        public static MvcHtmlString GridDetailAjax<TM, T>(this MnGridBuilder<TM> masterGrid, string controllerName, string refProperty)
            where T : BaseEntity
            where TM : BaseEntity
        {
            masterGrid.ClientDetailTemplateId("template");
            masterGrid.Events(events => events.DataBound("dataBound"));
            //var grid = GridMaster<T>(helper, controllerName);
            var grid = new MnWidgetFactory(masterGrid.Helper).GridDetailServer<T>(controllerName, refProperty);

            var str = MvcHtmlString.Create(string.Format(@"<div> {0} </div>
            <script id='template' type='text/kendo-tmpl'> 
            {1}
            </script>
            <script>                
                function dataBound() {{ 
                    this.expandRow(this.tbody.find('tr.k-master-row').first());
                }}
            </script>", masterGrid.ToClientTemplate().ToHtmlString().Replace("<\\/script>", "</script>"),
                      grid.ToClientTemplate().ToHtmlString()));
            return str;
        }


        public static MnWidgetFactory Mn(this HtmlHelper helper)
        {
            return new MnWidgetFactory(helper);
        }
    }
}
